import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersistense from 'vuex-persistedstate'

Vue.use(Vuex);

const vuexLocal = new VuexPersistense({ // 持久化的状态
  storage: localStorage,
  reducer: data => {
    return {
      //引入app模板，对象里面可配置需要持久化的status
      persistenceData: data
    }
  }
});

const store = new Vuex.Store({
  state: {
    user: {
      id: 0,
      username: '',
      mobile: '',
      email: '',
      sex: '',
      age: 18,
      password: ''
    }
  },
  plugins: [vuexLocal],
  mutations: {
    login: (state, payload) => {
      state.user = {...state.user, ...payload};
    },
    logout: (state) => {
      state.loginInfo = {
        id: 0,
        username: '',
        mobile: '',
        email: '',
        sex: '',
        age: 18,
        password: ''
      }
    }
  }
});

export default store;
